﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.IO;
using CF.Data;
using CF.Service;
//using llano.service;
//using llano.web;
//using llano.db;

namespace CF.Web
{
    public partial class CF : System.Web.UI.Page
    {
        string pjName = string.Empty;
        string tbName = string.Empty;
        string strColumn = "";
        string strGetSet = "";
        string strQueryColumnName = "";
        private static DataTable dtCol;
        protected void Page_Load(object sender, EventArgs e)
        {
            //string rqData = Request.Params["data"];
            pjName = Request.Params["pjName"];
            tbName = Request.Params["tbName"];
            object par = Request.Params;
            DataTable tb = getTable(pjName, tbName);
            if (pjName != "")
            {
                CreateCS(pjName, tbName);
            }
        }

        public DataTable getTable(string pjName,string tbName)
        {
            DataTable tb=null;
            //IDatabaseHelper db = DatabaseMgr.Instance.GetDatabaseHelper("");
            //if(tbName!="")
            //    tb = db.LoadDataTable(db.GetSqlStringCommand("select * from cf_column where 1=1 and projectName = '" + pjName + "' and tableName = '"+tbName+"'"));
            //else
            //    tb = db.LoadDataTable(db.GetSqlStringCommand("select * from cf_column where 1=1 and projectName = '" + pjName + "'"));
            return tb;
        }

        public void CreateExtAspx()
        {

        }

        private void CreateCS(string projectName, string tableName)
        {
            bool textEmpty = false;
            dtCol = getTable(projectName, tableName);

            string columnMemo = string.Empty; string strLenth = string.Empty;
            string strProjectName = dtCol.Rows[0]["projectName"].ToString();
            string strTableName = dtCol.Rows[0]["tableName"].ToString();

            for (int j = 0; j < dtCol.Rows.Count; j++)
            {
                if (dtCol.Rows[j]["columnName"].ToString() != "" && dtCol.Rows[j]["columnType"].ToString() != "")
                {
                    if (dtCol.Rows[j]["columnMemo"].ToString() != "")
                    {
                        columnMemo = "//" + dtCol.Rows[j]["columnMemo"].ToString();
                    }
                    strLenth = string.Empty;
                    if (dtCol.Rows[j]["lenth"].ToString() != "" && dtCol.Rows[j]["lenth"].ToString() != null)
                    {
                        strLenth = "\r\n\t\t[Column(Name = \"" + dtCol.Rows[j]["columnName"].ToString() + "\", DbType = \"" + dtCol.Rows[j]["lenth"].ToString() + "\")]";
                    }
                    strColumn += "\r\n\t\tprivate " + dtCol.Rows[j]["columnType"].ToString() + " _" + dtCol.Rows[j]["columnName"].ToString() + ";" + columnMemo;
                    strGetSet += strLenth + "\r\n\t\tpublic " + dtCol.Rows[j]["columnType"].ToString() + " " + dtCol.Rows[j]["columnName"].ToString() + "\r\n\t\t{\r\n\t\t\tget { return _" + dtCol.Rows[j]["columnName"].ToString() + "; }\r\n\t\t\tset { _" + dtCol.Rows[j]["columnName"].ToString() + " = value; }\r\n\t\t}";
                    strQueryColumnName += "\r\n\t\t\tpropName = \"" + dtCol.Rows[j]["columnName"].ToString() + "\";" + columnMemo + "\r\n\t\t\tif (ps[propName] != null && ps[propName].Length > 0)\r\n\t\t\t{\r\n\t\t\t\tparts.Add(BaseHelper.MakeCriteria(propName, \" = \"));\r\n\t\t\t\tparameters.Add(propName, ps[propName]);\r\n\t\t\t}";
                    columnMemo = string.Empty;
                }
                else
                {
                    textEmpty = true;
                }
            }
            //Response.Write("(i - 5) / 2=" + (i - 5) / 2 + " i="+i);
            //Response.Write(strProjectName);
            //Response.Write(strTableName);
            //Response.Write(strColumn);
            if (!textEmpty)
            {
                WriteDataFile(strProjectName, strTableName, strColumn, strGetSet);
                WriteQueryFile(strProjectName, strTableName, strQueryColumnName);
                WriteFacadeFile(strProjectName, strTableName);
                WriteFacadeCSFile(strProjectName, strTableName);
            }
            else
            {
                Response.Write("<script language=\"javascript\">alert(\"错误！不允许文本框为空！\")</script>");
            }
            //foreach (string key in keys)
            //{
            //    Response.Write("<br>" + key + ":");
            //    //if (key.StartsWith("ColumnName"))
            //    //{
            //    strColumn += "private " + Request.Form["ColumnType2"] + " " + Request.Form["ColumnName2"] + ";";

            //    //}
            //    Response.Write(strColumn);
            //}
        }

        //WriteDataFile 
        public static bool WriteDataFile(string strProjectName, string strTableName, string strColumn, string strGetSet)
        {
            string path = HttpContext.Current.Server.MapPath("~\\cs\\");
            Encoding code = Encoding.GetEncoding("gb2312");
            //如果目录不存在,将创建目录
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            // 读取模板文件 
            string temp = HttpContext.Current.Server.MapPath("~\\model.htm");
            StreamReader sr = null;
            StreamWriter sw = null;
            string str = "";
            try
            {
                sr = new StreamReader(temp, code);
                str = sr.ReadToEnd(); // 读取文件 
            }
            catch (Exception exp)
            {
                HttpContext.Current.Response.Write(exp.Message);
                HttpContext.Current.Response.End();
                sr.Close();
            }


            string csFileName = "T" + strTableName + ".cs";
            // 替换内容 
            // 这时,模板文件已经读入到名称为str的变量中了 
            str = str.Replace("ProjectName", strProjectName); //模板页中的ShowArticle 
            str = str.Replace("TableName", strTableName);
            str = str.Replace("ColumnName", strColumn);
            str = str.Replace("GetSet", strGetSet);
            // 写文件 
            try
            {
                sw = new StreamWriter(path + csFileName, false, code);
                sw.Write(str);
                sw.Flush();
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.Message);
                HttpContext.Current.Response.End();
            }
            finally
            {
                sw.Close();
            }
            return true;

            //此函数放在Conn.CS基类中了 
            //在添加新闻的代码中引用 注：工程名为Hover 

            //if(Hover.Conn.WriteDataFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString))) 
            //{ 
            //    Response.Write("添加成功"); 
            //} 
            //else 
            //{ 
            //    Response.Write("生成HTML出错!"); 
            //} 

        }
        //WriteQueryFile
        public static bool WriteQueryFile(string strProjectName, string strTableName, string strQueryColumnName)
        {
            string path = HttpContext.Current.Server.MapPath("~\\cs\\");
            Encoding code = Encoding.GetEncoding("gb2312");
            //如果目录不存在,将创建目录
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            // 读取模板文件 
            string temp = HttpContext.Current.Server.MapPath("~\\model2.htm");
            StreamReader sr = null;
            StreamWriter sw = null;
            string str = "";
            try
            {
                sr = new StreamReader(temp, code);
                str = sr.ReadToEnd(); // 读取文件 
            }
            catch (Exception exp)
            {
                HttpContext.Current.Response.Write(exp.Message);
                HttpContext.Current.Response.End();
                sr.Close();
            }


            string csFileName = strTableName + "Query.cs";
            // 替换内容 
            // 这时,模板文件已经读入到名称为str的变量中了 
            str = str.Replace("ProjectName", strProjectName); //模板页中的ShowArticle 
            str = str.Replace("TableName", strTableName);
            str = str.Replace("QueryColumnName", strQueryColumnName);
            // 写文件 
            try
            {
                sw = new StreamWriter(path + csFileName, false, code);
                sw.Write(str);
                sw.Flush();
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.Message);
                HttpContext.Current.Response.End();
            }
            finally
            {
                sw.Close();
            }
            return true;
        }
        //WriteFacadeCSFile
        public static bool WriteFacadeCSFile(string strProjectName, string strTableName)
        {
            string path = HttpContext.Current.Server.MapPath("~\\cs\\");
            Encoding code = Encoding.GetEncoding("gb2312");
            //如果目录不存在,将创建目录
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            // 读取模板文件 
            string temp = HttpContext.Current.Server.MapPath("~\\model3.htm");
            StreamReader sr = null;
            StreamWriter sw = null;
            string str = "";
            try
            {
                sr = new StreamReader(temp, code);
                str = sr.ReadToEnd(); // 读取文件 
            }
            catch (Exception exp)
            {
                HttpContext.Current.Response.Write(exp.Message);
                HttpContext.Current.Response.End();
                sr.Close();
            }


            string csFileName = strTableName + ".aspx.cs";
            // 替换内容 
            // 这时,模板文件已经读入到名称为str的变量中了 
            str = str.Replace("ProjectName", strProjectName); //模板页中的ShowArticle 
            str = str.Replace("TableName", strTableName);
            // 写文件 
            try
            {
                sw = new StreamWriter(path + csFileName, false, code);
                sw.Write(str);
                sw.Flush();
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.Message);
                HttpContext.Current.Response.End();
            }
            finally
            {
                sw.Close();
            }
            return true;
        }
        //WriteFacadeFile
        public static bool WriteFacadeFile(string strProjectName, string strTableName)
        {
            string path = HttpContext.Current.Server.MapPath("~\\cs\\");
            Encoding code = Encoding.GetEncoding("gb2312");
            //如果目录不存在,将创建目录
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            // 读取模板文件 
            string temp = HttpContext.Current.Server.MapPath("~\\model4.htm");
            StreamReader sr = null;
            StreamWriter sw = null;
            string str = "";
            try
            {
                sr = new StreamReader(temp, code);
                str = sr.ReadToEnd(); // 读取文件 
            }
            catch (Exception exp)
            {
                HttpContext.Current.Response.Write(exp.Message);
                HttpContext.Current.Response.End();
                sr.Close();
            }


            string csFileName = strTableName + ".aspx";
            // 替换内容 
            // 这时,模板文件已经读入到名称为str的变量中了 
            str = str.Replace("ProjectName", strProjectName); //模板页中的ShowArticle 
            str = str.Replace("TableName", strTableName);
            // 写文件 
            try
            {
                sw = new StreamWriter(path + csFileName, false, code);
                sw.Write(str);
                sw.Flush();
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.Message);
                HttpContext.Current.Response.End();
            }
            finally
            {
                sw.Close();
            }
            return true;
        }
    }
}
